﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLocalChurch.Constants;
using MyLocalChurch.Models.Enumerations;
using MyLocalChurch.Models.KidsChurch;
using RKConsulting.Helpers;

namespace MyLocalChurch.Business
{
    public static class KidManager
    {
        public static Kid GetKid(MyContext context, int kidId)
        {
            return context.Kids.FirstOrDefault(k => k.Id == kidId);
        }

        public static Kid GetKid(MyContext context, string barcode)
        {
            if (String.IsNullOrEmpty(barcode))
                return null;

            var kids = context.Kids.ToList();
            foreach (var k in kids)
            {
                var validBarcode = BarcodeManager.GetValidBarcode(k.Barcodes);
                if (validBarcode == null || validBarcode.Code != barcode)
                    continue;

                return k;
            }

            return null;
        }

        public static KidCategory GetKidCategory(Kid kid)
        {
            return GetKidCategory(kid.BirthDate);
        }

        public static KidCategory GetKidCategory(DateTime birthDate)
        {
            var age = DateHelper.ComputeAge(birthDate);

            if (age <= KidsChurchConstants.ToddlerMaxAge)
                return KidCategory.Toddler;
            if (age <= KidsChurchConstants.PreSchoolMaxAge)
                return KidCategory.PreSchool;
            if (age <= KidsChurchConstants.PrimaryMaxAge)
                return KidCategory.Primary;
            if (age <= KidsChurchConstants.PreTeenMaxAge)
                return KidCategory.PreTeen;

            return KidCategory.NotAKid;
        }
    }
}